@extends('app')

@section('content')
    <div class="layui-card" style="margin-bottom: 0;">
        @include('layouts.breadcrumb')

        <div class="layui-tab-content">
            <form class="layui-form">
                <blockquote class="layui-elem-quote quoteBox" >
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="">状态</option>
                                <option value="1" @if($status==1) selected @endif>上架</option>
                                <option value="2" @if($status==2) selected @endif>下架</option>
                                <option value="3" @if($status==3) selected @endif>申请中</option>
                            </select>
                        </div>
                    </div>
                    <a class="layui-btn search_btn" data-type="reload" lay-submit lay-filter="search" style="margin-left: 3px"><i class="layui-icon layui-icon-search"></i>查询</a>
                </blockquote>
            </form>
            <div class="layui-tab-item layui-show">
                <table class="layui-hide" id="model" lay-filter="model"></table>
            </div>
        </div>
    </div>

    {{--表头操作--}}
    <script type="text/html" id="upload">
        <a class="layui-btn layui-btn-sm" lay-event="add">新增</a>
    </script>
    {{--表列操作--}}
    <script type="text/html" id="operate">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    </script>
@endsection

@section('js')
    <script>
        layui.config({
            version:false
        }).use('table', function(){
            var table = layui.table;
            var form = layui.form;
            var $   = layui.$;

            form.on('submit(search)', function(data){
                var field = data.field;
                //执行重载
                table.reload('model', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: field
                });
            });

            //上架/下架
            form.on('switch(component-form-status)', function(data){
                var status = this.checked ? '1' : '2';
                var id = $(this).attr('data-id');
                $.ajax({
                    type: 'POST',
                    url: '/links/status',
                    data:{id:id,status:status},
                    dataType: 'json',
                    success: function(data){
                        layer.msg(data.msg,{time:2000});
                    }
                });
            });

            table.render({
                elem: '#model',
                url:'/links/index',
                cellMinWidth: 95, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                height: 'full-150',
                method : 'POST',
                page: true, //开启分页
                defaultToolbar: ['filter', 'print', 'exports'],
                toolbar:'#upload',
                limit:20,
                loading:true,
                rowStyle: function (index, item) {//index第几行，row当前行的值
                    return 'data-id="'+item.id+'" ';
                },
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field:'id', title: 'ID',align:'center'},
                    {field:'title',width:'180',title: '网站名称'},
                    {field:'link',width:'250',title: '网站地址',templet:function(item){
                            return '<a class="layadmin-user-jump-change layadmin-link" href="'+item.link+'" target="_blank">'+item.link+'</a>';
                        }
                    },
                    {field:'status',title: '状态',templet:function(row){
                            // return item.status==1?'<span style="color:green">上架</span>':(item.status==2?'<span style="color:red">下架</span>':'申请中')
                            if(row.status==1){
                                return '<input type="checkbox" name="status" data-id='+row.id+' lay-filter="component-form-status" lay-skin="switch" checked lay-text="上架|下架">';
                            }else{
                                return '<input type="checkbox" name="status" data-id='+row.id+' lay-filter="component-form-status" lay-skin="switch" lay-text="上架|下架">';
                            }
                        }
                    },
                    {field:'contact',width:'250',title: '联系方式'},
                    {field:'remark',width:'450',title: '申请介绍'},
                    {field:'updated_at',width:'180',title: '更新时间',templet:function(item){return dateTime(item.updated_at)}},
                    {title: '操作',width:100, align:'center', fixed: 'right', toolbar: '#operate'}
                ]]
            });

            //表头操作
            table.on('toolbar(model)', function(obj){
                switch(obj.event){
                    case 'add':
                        newWindow('新增友链', '/links/edit','500','350');
                        break;
                };
            });

            //列操作
            table.on('tool(model)',function (obj) {
                if(obj.event === 'del'){//删除行
                    del(obj);
                }else if(obj.event==="edit"){
                    var data = obj.data;
                    newWindow('编辑友链','/links/edit?id='+data.id,500,350);
                }
            });
        })
    </script>
@endsection